package com.futao.eurekaconsumer.service.Hystrix;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author futao
 * Created on 18-6-6-下午1:46.
 * 超时时间参数等可以在application.yml中配置
 */
@RestController
public class HystrixController {

    @Autowired
    ConsumerService consumerService;

    @GetMapping("/hystrixConsumer")
    public String HystrixController() {
        return consumerService.consumer();
    }

    @Component
    class ConsumerService {

        @Autowired
        RestTemplate restTemplate;

        @HystrixCommand(fallbackMethod = "fallback")
        public String consumer() {
            return restTemplate.getForObject("http://springBootTestProject/users/", String.class);
        }

        public String fallback() {
            return "fallback";
        }

    }

}
